<?php

class DataBaseManager
{
	static private $lienBDD = null;
	static private $instance = null;

	public static function getInstance()
	{
		if(self::$instance == null)
			self::$instance = new self;
			
		return self::$instance;
	}

	public function closeInstance()
	{
		self::$instance->__destruct();
	}

	private function __construct()
	{
		try
		{
			self::$lienBDD = mysqli_init();
			mysqli_real_connect(self::$lienBDD, "127.0.0.1", "root", "", "ecommerce") or die("Impossible de se connecter à la base de donnée");
		}catch(Exception $e){die($e->getMessage());}
	}

	public function __destruct()
	{
		if(self::$instance != null)
		{
			mysqli_close(self::$lienBDD);
			self::$instance = null;
			self::$lienBDD = null;
		}
	}

	public function execRequete($requete)
	{
		try{
			$result = mysqli_query(self::$lienBDD, $requete) or die ("Erreur lors de l'éxécution de la requête : ".mysqli_error(self::$lienBDD));
		}catch(Exception $e){$result = false;}
		
		return $result;
	}
	
	public function libereResultatRequete($result)
	{
		if(strcmp(gettype($result), 'Object') == 0 &&
			strcmp(get_class($result), 'mysqli_result') == 0)
			mysqli_free_result($result);
	}
	
}
?>	